using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using cframework;
using System.ComponentModel;
using Newtonsoft.Json;
namespace cframework
{
    [CreateAssetMenu(menuName = "cframework/DeviceToolPathSetting")]
    public class DeviceToolPathSetting : ScriptableObject
    {
        public SerializableDictionary<string, SerializableDictionary<string, string>> deveiceToolPaths = new SerializableDictionary<string, SerializableDictionary<string, string>>();

        [ContextMenu("Add this device")]
        void AddThisDevice()
        {
            var tools = GetToolPaths();
            if (tools == null)
            {
                tools = new SerializableDictionary<string, string>();
                deveiceToolPaths.Add(UnityEngine.SystemInfo.deviceName, tools);
            }
        }

        public SerializableDictionary<string, string> GetToolPaths()
        {
            string deviceName = UnityEngine.SystemInfo.deviceName;
            SerializableDictionary<string, string> tools = null;
            deveiceToolPaths.TryGetValue(deviceName, out tools);
            return tools;
        }


        public string GetToolPath(string toolName)
        {
            var tools = GetToolPaths();
            return GetToolPath(tools, toolName);
        }

        public string GetCmdToolPath(string toolName, string cmd = null)
        {
            var tools = GetToolPaths();
            return GetCmdToolPath(tools, toolName, cmd);
        }

        public static string GetToolPath(SerializableDictionary<string, string> toolPaths, string toolName)
        {
            string value = null;
            if (toolPaths == null || !toolPaths.TryGetValue(toolName, out value))
            {
                return null;
            }

            return value;
        }

        public static string GetCmdToolPath(SerializableDictionary<string, string> toolPaths, string toolName, string cmd = null)
        {
            string a = GetToolPath(toolPaths, toolName);
            if (string.IsNullOrEmpty(a))
            {
                a = GetSystemToolPath(toolName, cmd);
            }

            return a;
        }

        public static string GetSystemToolPath(string toolName, string cmd = null)
        {
            if (string.IsNullOrEmpty(cmd))
            {
                cmd = Application.platform == RuntimePlatform.WindowsEditor ? "where" : "which";
            }

            string a = null;
            var ret = CommandLineTool.RunCommand2(cmd, toolName, ".");
            if (ret.ExitCode != 0)
            {
                //Debug.LogError("output: " + ret.StdOut);
                //Debug.LogError("error: " + ret.StdErr);
            }
            else
            {
                a = ret.StdOut.Trim();

                if (a.Contains("\n"))
                {
                    a = a.Split("\n")[0].Trim();
                }

                if (!CommonUtil.Exists(a))
                {
                    a = null;
                }
            }

            return a;
        }
    }
}